﻿using System;
using System.Text;
using System.Collections.Generic;
using Sce.PlayStation.Core;
using Sce.PlayStation.Core.Imaging;
using Sce.PlayStation.Core.Environment;
using Sce.PlayStation.HighLevel.UI;
using VitaReader;

namespace VitaReaderLibPS
{	
    public partial class ReaderScene : Scene
    {
		private  IReaderView mView;
		internal ReaderLanguage mLang;
		
		/*
		private ImageAsset				  mIconCross;
		private ImageAsset				  mIconTriangle;
		private ImageAsset				  mIconSquare;
		private ImageAsset				  mIconCircle;
		*/
		
		internal ImageAsset	mBackground;
		internal CustomButtonImageSettings mButtonImage;
		internal CustomButtonImageSettings mHiLightButtonImage;
		
		//internal CustomButtonImageSettings mListImage;
		//internal CustomButtonImageSettings mHiLightListImage;
		
		internal CustomCheckBoxImageSettings mCheckImage;
		
		internal ImageAsset	mDownloadIconUpdated;
		internal ImageAsset	mDownloadIconNew;
		internal ImageAsset	mDownloadIconDifferent;
		
		internal UIColor	mButtonTextColor;
		internal UIFont		mButtonTextFont;
		
		internal UIColor	mTitleColor;
		internal UIFont 	mTitleFont;
		
		internal UIColor	mListTextColor;
		internal UIFont		mListTextFont;
		
		internal ImageAsset mListBackground;
		internal UIColor	mListBackColor;
		
		internal UIColor	mListItemColor;
		internal UIColor	mListItemHilightColor;
		
		internal UIColor	mDialogBackground;
		
		internal List<ReaderUI>	mUIList;
		internal BookInfo mCurrentBook;
		internal IReadingHandler mReadingHandler;
		
        public ReaderScene(IReaderView view)
        {
			mView = view;
			
			mLang = new ReaderLanguage();
			
			/*
			mIconCross = new ImageAsset("/Application/assets/cross.png");
			mIconTriangle = new ImageAsset("/Application/assets/triangle.png");
			mIconSquare = new ImageAsset("/Application/assets/square.png");
			mIconCircle = new ImageAsset("/Application/assets/circle.png");
			*/
			mBackground = new ImageAsset("/Application/assets/Reader.png");
			
			//pre-create button image 
			mButtonImage = new CustomButtonImageSettings();
            mButtonImage.BackgroundNormalImage = new ImageAsset("/Application/assets/Button.png");
            mButtonImage.BackgroundPressedImage = new ImageAsset("/Application/assets/ButtonPressed.png");
            mButtonImage.BackgroundDisabledImage = new ImageAsset("/Application/assets/Button.png");
            mButtonImage.BackgroundNinePatchMargin = new NinePatchMargin(20, 10, 55, 15);
			
			mHiLightButtonImage = new CustomButtonImageSettings();
			mHiLightButtonImage.BackgroundNormalImage = mButtonImage.BackgroundPressedImage;
			mHiLightButtonImage.BackgroundPressedImage = mButtonImage.BackgroundNormalImage;
			mHiLightButtonImage.BackgroundDisabledImage = mButtonImage.BackgroundPressedImage;
			mHiLightButtonImage.BackgroundNinePatchMargin = mButtonImage.BackgroundNinePatchMargin;
			
			/*
			mListImage = new CustomButtonImageSettings();
            mListImage.BackgroundNormalImage = new ImageAsset("/Application/assets/ButtonBook.png");
            mListImage.BackgroundPressedImage = new ImageAsset("/Application/assets/ButtonBookPressed.png");
            mListImage.BackgroundDisabledImage = new ImageAsset("/Application/assets/ButtonBook.png");
            mListImage.BackgroundNinePatchMargin = new NinePatchMargin(5, 5, 5, 5);
			
			mHiLightListImage = new CustomButtonImageSettings();
            mHiLightListImage.BackgroundNormalImage = mListImage.BackgroundPressedImage;
            mHiLightListImage.BackgroundPressedImage = mListImage.BackgroundNormalImage;
            mHiLightListImage.BackgroundDisabledImage = mListImage.BackgroundPressedImage;
            mHiLightListImage.BackgroundNinePatchMargin = new NinePatchMargin(5, 5, 5, 5);
			*/
			mListBackground = new ImageAsset("/Application/assets/ListBk.png");
			
			mCheckImage = new CustomCheckBoxImageSettings();
			mCheckImage.NormalUncheckedImage = new ImageAsset("/Application/assets/unchecked.png");
			mCheckImage.NormalCheckedImage = new ImageAsset("/Application/assets/checked.png");
			mCheckImage.PressedUncheckedImage = mCheckImage.NormalUncheckedImage;
			mCheckImage.PressedCheckedImage = mCheckImage.NormalCheckedImage;
			mCheckImage.DisabledCheckedImage = mCheckImage.NormalCheckedImage;
			mCheckImage.DisabledUncheckedImage = mCheckImage.NormalUncheckedImage;
			
			mDownloadIconUpdated = new ImageAsset( "/Application/assets/icon_updated.png" );
			mDownloadIconNew = new ImageAsset( "/Application/assets/icon_new.png" );
			mDownloadIconDifferent = new ImageAsset( "/Application/assets/icon_different.png" );

			
			mButtonTextColor = new UIColor(74f / 255f, 78f / 255f, 35f / 255f, 255f / 255f);
			mButtonTextFont = new UIFont( FontAlias.System, 25, FontStyle.Regular);
			
			mTitleColor = new UIColor(0,0,0,1);
			mTitleFont = new UIFont( FontAlias.System, 35, FontStyle.Bold);
			mListBackColor = new UIColor(0,0,0,0.5f);
			
			mListTextColor = new UIColor(100f / 255f, 40f / 255f, 20f / 255f, 255f / 255f);
			mListTextFont = (mView as PSView).FontSet.Middle;
			
			mListItemColor = new UIColor(0.3f,0.3f,0.3f,0.5f);
			mListItemHilightColor = new UIColor(1,1,1,0.5f);
			
			mDialogBackground = new UIColor(1.0f,1.0f,1.0f,0.95f);

            this.InitializeWidget();
            this.UpdateLanguage();
			this.SetWidgetLayout(LayoutOrientation.Horizontal);
			//UISystem.FocusActive = true;
			this.ShowUI(UIType.MAINMENU);
        }
		
		
		public ReaderLanguage	Language	{get {return mLang;}}
		
		
		#region ui properties
		public UIColor	ButtonTextColor		{get {return mButtonTextColor;}}
		public UIFont ButtonTextFont		{get {return mButtonTextFont;}}
		public UIFont TitleFont				{get {return mTitleFont;}}
		public UIColor TitleColor			{get {return mTitleColor;}}
		public UIColor ListItemTextColor	{get {return mListTextColor;}}
		public UIFont ListItemTextFont		{get {return mListTextFont;}}
		public ImageAsset	BackgroundImage	{get {return mBackground;}}
		public CustomButtonImageSettings ButtonImage	{get { return mButtonImage;}}
		public CustomButtonImageSettings HilightButtonImage	{get { return mHiLightButtonImage;}}
		//public CustomButtonImageSettings ListImage			{get { return mListImage;}}
		//public CustomButtonImageSettings HilightListImage	{get { return mHiLightListImage;}}
		public ImageAsset ListBackgroundImage{get {return mListBackground;}}
		CustomCheckBoxImageSettings	CheckBoxImage {get {return mCheckImage;}}
		public ImageAsset	StateIconNew	{get {return mDownloadIconNew;}}
		public ImageAsset	StateIconUpdated{get {return mDownloadIconUpdated;}}
		public ImageAsset	StateIconDifferent{get {return mDownloadIconDifferent;}}

		public UIColor ListBackgroundColor					{get {return mListBackColor;}}
		public UIFontSet FontSet							{get {return (mView as PSView).FontSet;}}
		public UIFont TextFont								{get {return (mView as PSView).Font;}}
		public UIColor ListItemColor		{get {return mListItemColor;}}
		public UIColor ListItemHilightColor {get {return mListItemHilightColor;}}
		public UIColor DialogBackgroundColor{get {return mDialogBackground;}}
		#endregion
		
		#region Event handlers
		public void HandleShowMenu(object sender, TouchEventArgs e)
		{
			this.ShowUI(UIType.MAINMENU);
		}
		
		public void HandleContinue(object sender, TouchEventArgs e)
		{
			if( this.Handler.ReadingContinue(UISwitch.NONE) )
				return;
			
			if( this.Data.LastReadBook != "" && !IsHelp(this.Data.LastReadBook) )
			{
				Handler.ReadingOpen(this.Data.LastReadBook);
			}
			else
				this.ShowUI(UIType.BOOKS);
		}
		
		public void HandleBookList(object sender, TouchEventArgs e)
		{
			this.ShowUI(UIType.BOOKS);
		}
		
		public void HandleHelp(object sender, TouchEventArgs e)
		{
			Handler.ReadingHelp();
		}
		
		public void HandleExit(object sender, TouchEventArgs e)
		{
			Handler.ReadingClose();
			//we don't care how the control do it
			this.Control.Exit();
		}
		#endregion
		
		#region message box
		public void ShowMessageBox(String title,String msg)
		{
			MessageBox box = new MessageBox(title,msg,this);
			box.Show();
		}
		
		public void ConfirmMessageBox(String msg, MessageBox.OnOKDelegate d, object o)
		{
			MessageBox box = new MessageBox(this.Language.TitleConfirm,msg,this, MessageDialogStyle.OkCancel, d, o);
			box.Show();
		}
		#endregion
		
		public IReadingHandler Handler	{get {return mReadingHandler;}}
		public IReaderControl Control	{get {return mView.Owner.Control;}}
		public IReaderView View			{get {return mView;}}
		public IReaderData Data			{get {return mView.Owner.Data;}}
		public IReader Reader			{get {return mView.Owner;}}
		public BookInfo CurrentBook		{get {return mCurrentBook;}	set {mCurrentBook = value;}}
		
		public bool IsHelp(String fileName)
		{
			return fileName.StartsWith("__help__.");
		}
		
		public bool IsReadingHelp()
		{
			return this.Data.LastReadBook != null && IsHelp(this.Data.LastReadBook);
		}
		
		public void Update()
		{
			foreach(ReaderUI ui in mUIList)
			{
				if( ui != null )
					ui.Update();
			}
		}
    }//class
}
